/**
 *
 * 　　　┏┓　　　┏┓
 * 　　┏┛┻━━━┛┻┓
 * 　　┃　　　　　　　┃
 * 　　┃　　　━　　　┃
 * 　　┃　┳┛　┗┳　┃
 * 　　┃　　　　　　　┃
 * 　　┃　　　┻　　　┃
 * 　　┃　　　　　　　┃
 * 　　┗━┓　　　┏━┛
 * 　　　　┃　　　┃神兽保佑
 * 　　　　┃　　　┃代码无BUG！
 * 　　　　┃　　　┗━━━┓
 * 　　　　┃　　　　　　　┣┓
 * 　　　　┃　　　　　　　┏┛
 * 　　　　┗┓┓┏━┳┓┏┛
 * 　　　　　┃┫┫　┃┫┫
 * 　　　　　┗┻┛　┗┻┛
 * ━━━━━━神兽出没━━━━━━coder-zhouxiongwei
 */
/**
 * Created by zhouxiongwei on 2019.03.25
 */

var express = require('express');
var router = express.Router();
var ScalpingDao = require('../dao/scalping');
var MallDao = require('../dao/mall');
var UsersDao = require('../dao/users');
var ScalpingRouters = module.exports;

//筛选分类功能
function change_keyword(aid) {
    aid == '0' ? aid : '';
    aid == '1' ? aid = '家具百货' : aid;
    aid == '2' ? aid = '家用电器' : aid;
    aid == '3' ? aid = '数码家电' : aid;
    aid == '4' ? aid = '生活居家' : aid;
    aid == '5' ? aid = '服装配饰' : aid;
    aid == '6' ? aid = '食品酒水' : aid;
    aid == '7' ? aid = '美妆个护' : aid;
    aid == '8' ? aid = '钟表珠宝' : aid;
    aid == '9' ? aid = '运动户外' : aid;
    aid == '10' ? aid = '鞋靴箱包' : aid;
    aid == '11' ? aid = '母婴童装' : aid;
    aid == '12' ? aid = '生活家居' : aid;

    return aid;
}

//分类换取利润比例
function rank_to_voucher(rank) {
    rank == '1' ? rank = 0.02 : rank;
    rank == '2' ? rank = 0.35 : rank;
    rank == '3' ? rank = 0.15 : rank;
    rank == '4' ? rank = 0.20 : rank;

    return rank;
}

//sid换取平台名称
function change_appSid(app_sid) {
    app_sid == 'FTK' ? app_sid = '菲淘客' : app_sid;

    return app_sid;
}

//获取json对象长度
function getJsonObjLength(jsonObj) {
    var Length = 0;
    for (var item in jsonObj) {
        Length++;
    }
    return Length;
}

function add0(m) {
    return m < 10 ? '0' + m : m
}

//时间戳格式化
function format(shijianchuo) {
    //shijianchuo是整数，否则要parseInt转换
    var time = new Date(shijianchuo);
    var y = time.getFullYear();
    var m = time.getMonth() + 1;
    var d = time.getDate();
    var h = time.getHours();
    var mm = time.getMinutes();
    var s = time.getSeconds();
    return y + '-' + add0(m) + '-' + add0(d) + ' ' + add0(h) + ':' + add0(mm) + ':' + add0(s);
}

ScalpingRouters.get_access_token = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_access_token(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            return res.json(new PKG(data1));
        }
    });
}
router.post('/get_access_token', ScalpingRouters.get_access_token);

ScalpingRouters.get_access_token_wb = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_access_token_wb(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            return res.json(new PKG(data1));
        }
    });
}
router.post('/get_access_token_wb', ScalpingRouters.get_access_token_wb);

ScalpingRouters.get_user_token = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_user_token(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            return res.json(new PKG(data1));
        }
    });
}
router.post('/get_user_token', ScalpingRouters.get_user_token);

ScalpingRouters.get_token_info = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_token_info(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            return res.json(new PKG(data1));
        }
    });     
}
router.post('/get_token_info', ScalpingRouters.get_token_info);

ScalpingRouters.get_user_info = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_user_info(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            data1 = JSON.parse(data1);
            var user = {
                wbid: data1.id,
                screen_name: data1.screen_name,
                name: data1.name,
                location: data1.location,
                profile_image_url: data1.profile_image_url,
                cover_image_phone: data1.cover_image_phone,
                profile_url: data1.profile_url,
                followers_count: data1.followers_count,
                friends_count: data1.friends_count,
                created_date: format(data1.created_at),
                avatar_large: data1.avatar_large,
                update_date: new Date()
            }

            ScalpingDao.check_wbid_exist(user, function(err, data){
                if (err) {
                    return res.json(new ERR(err, 410));
                } else {
                    if (data.status == 200) {
                        ScalpingDao.saveWbUsers(user, function (err, data) {
                            if (err) {
                                return res.json(new ERR(err, 410));
                            } else {
                                if (data.status == 200) {
                                    user.comment = '保存用户数据到数据库成功!';
                                    return res.json(new PKG(user));
                                } else if (data.status == 201) {
                                    return res.json(new ERR(data, 420));
                                }
                            }
                        });
                    } else if (data.status == 201) {
                        return res.json(new ERR(user, 420));
                    }
                }
            });
        }
    });
}
router.post('/get_user_info', ScalpingRouters.get_user_info);

//查看自己所得分润
ScalpingRouters.get_user_profit = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('用户手机号不为空!', 400));
    }

    ScalpingDao.get_user_aliaccount(par, function (err1, data1) {
        if (err1) {
            return res.json(new ERR(err1, 410));
        } else {
            if (data1.status == 200) {
                par.ali_account = data1.userInfo.ali_account;

                ScalpingDao.get_user_profit(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            return res.json(new PKG(data));
                        } else if (data.status == 201) {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data1.status == 201) {
                return res.json(new ERR(data1, 420));
            }
        }
    });
}
router.post('/get_user_profit', ScalpingRouters.get_user_profit);

//同步金豆商城商品信息(每次更新一百条)
/**
 * 1.调取接口取到对方接口最新的数据，每次100条
 * 2.然后逐条判断商品id是否已经存在
 *  2.1判断id是否存在，不存在则插入
 *  2.2判断id是否存在，存在则判断库存量是否改变，改变则更新库存量
 */
ScalpingRouters.syncGoods = function (req, res) {
    var par = paramAll(req);

    if (!par.synckey) {
        par.synckey = 0;
    }

    ScalpingDao.syncGoods(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (par.status == 200) {
                //判断商品id是否已经存在
                data.data.forEach(data => {
                    if (data.publish_date == '0000-00-00 00:00:00') {
                        data.publish_date = new Date();
                    }
                    data.created_date = format(data.created_date);
                    data.publish_date = format(data.publish_date);
                    ScalpingDao.check_goods_exist(data, function (err1, data1) {
                        if (err1) {
                            return res.json(new ERR(err1, 410));
                        } else {
                            if (data1.status == 200) { //商品不存在，将上架更新到商品库
                                ScalpingDao.saveGoods(data, function (err2, data2) {
                                    if (err2) {
                                        return res.json(new ERR(err2, 410));
                                    } else {
                                        if (data2.status == 200) {} else if (data2.status == 201) {
                                            console.log(data2);
                                        }
                                    }
                                });
                            } else if (data1.status == 201) { //商品存在，判断商品库存量是否改变，改变则更新
                                if (data1.totalNum == data.totalNum) {
                                    console.log('商品已存在，库存量未更新!' + data.id);
                                } else {
                                    ScalpingDao.changed_totalNum(data, function (err2, data2) {
                                        if (err2) {
                                            return res.json(new ERR(err2, 410));
                                        } else {
                                            if (data2.status == 200) {
                                                console.log(data2);
                                            } else if (data2.status == 201) {
                                                console.log(data2);
                                            }
                                        }
                                    });
                                }
                            }
                        }
                    });
                });
                return res.json(new PKG(data));
            } else if (par.status == 201) { //当前无最新上架商品
                return res.json(new ERR(data));
            }
        }
    });
}
router.post('/sync_goods', ScalpingRouters.syncGoods);

//获取商品可以获得佣金
ScalpingRouters.get_tkRate = function (req, res) {
    var par = paramAll(req);

    if (!par.gid) {
        return res.json(new ERR('商品id不能为空!', 400));
    }

    ScalpingDao.get_tkRate(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/get_tkRate', ScalpingRouters.get_tkRate);

//通过订单佣金状态状态查询订单数据
ScalpingRouters.search_order_by_status = function (req, res) {
    var par = paramAll(req);

    if (!par.balance_status && !par.bussiness_status) {
        return res.json(new ERR('状态码不为空!', 400));
    }
    if (par.balance_status && par.bussiness_status) {
        return res.json(new ERR('状态码有误!', 400));
    }
    if (par.balance_status && !par.app_sid) {
        return res.json(new ERR('参数有误!', 400));
    }

    ScalpingDao.search_order_by_status(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/search_order_by_status', ScalpingRouters.search_order_by_status);

//查询订单接口
ScalpingRouters.show_goods_order = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('登录手机号不为空!', 400));
    }
    if (!par.showcase_status) {
        return res.json(new ERR('订单类型不能为空!', 400));
    }

    if (par.showcase_status == 1) {
        return res.json(new ERR('暂不支持查询商品物流信息!', 430));
    } else if (par.showcase_status == 2) {
        ScalpingDao.show_goods_order(par, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (data.status == 200) {
                    return res.json(new PKG(data));
                } else if (data.status == 201) {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    } else {
        return res.json(new ERR('订单类型有误!', 430));
    }
}
router.post('/show_goods_order', ScalpingRouters.show_goods_order);

//用户扫描海报图，选择收货地址时自动注册收货手机号
ScalpingRouters.auto_user_regist = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('用户手机号不为空!', 400));
    }
    if (!par.recommend_id) {
        return res.json(new ERR('推荐人手机号不能为空!', 400));
    }

    MallDao.get_user_recommendCode(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                par.recommendCode = uidToCode(data.recommendUid);
                var userInfo = {
                    account: par.user_id,
                    password: par.user_id,
                    app_sid: 'usuz',
                    recommendCode: par.recommendCode
                };
                console.log(userInfo);
                UsersDao.auto_user_regist(userInfo, function (err, data) { //将account注册，已注册的登录下
                    if (err) {
                        return res.json(new ERR(err, 200));
                    } else {
                        return res.json(new PKG(data));
                    }
                });
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/auto_user_regist', ScalpingRouters.auto_user_regist);    

//存取订单前先取到商品佣金值以及商户可得利润
ScalpingRouters.get_commission_profit = function (req, res) {
    var par = paramAll(req);

    if (!par.gid) {
        return res.json(new ERR('佣金状态码不能为空!', 400));
    }

    ScalpingDao.get_commission_profit(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/get_commission_profit', ScalpingRouters.get_commission_profit);

//添加购物车功能
ScalpingRouters.save_shopping_cart = function(req, res){
    var par = paramAll(req);

    if(!par.gid){
        return res.json(new ERR('商品id不能为空!', 400));
    }
    if(!par.title){
        return res.json(new ERR('店名不能为空!', 400));
    }
    if(!par.good_title){
        return res.json(new ERR('商品标题不能为空!', 400));
    }
    if(!par.zk_price){
        return res.json(new ERR('商品价格不能为空!', 400));
    }
    if(!par.score){
        return res.json(new ERR('兑换积分不能为空!', 400));
    }
    if(!par.user_id){
        return res.json(new ERR('用户账号不能为空!', 400));
    }
    if(!par.shop_img){
        return res.json(new ERR('商品图不能为空!', 400));
    }

    ScalpingDao.check_shopping_cart_exist(par, function(err, data){
        if(err){
            return res.json(new PKG(err, 410));
        }else{
            if(data.status == 200){
                ScalpingDao.save_shopping_cart(par, function(err, data){
                    if(err){
                        return res.json(new ERR(err, 410));
                    }else{
                        if(data.status == 200){
                            return res.json(new PKG(data));
                        }else if(data.status == 201){
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            }else if(data.status == 201){
                return res.json(new ERR(data, 420));
            }
        }
    });  
}
router.post('/save_shopping_cart', ScalpingRouters.save_shopping_cart);

//删除购物车功能
ScalpingRouters.delete_shopping_cart = function(req, res){
    var par = paramAll(req);

    if(!par.gid){
        return res.json(new ERR('商品id不能为空!', 400));
    }
    if(!par.user_id){
        return res.json(new ERR('用户账号不能为空!', 400));
    }

    ScalpingDao.delete_shopping_cart(par, function(err, data){
        if(err){
            return res.json(new PKG(err, 410));
        }else{
            if(data.status == 200){
                return res.json(new PKG(data));
            }else if(data.status == 201){
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/delete_shopping_cart', ScalpingRouters.delete_shopping_cart);

//查询购物车功能
ScalpingRouters.get_shopping_cart = function(req, res){
    var par = paramAll(req);

    if(!par.user_id){
        return res.json(new ERR('用户账号不能为空!', 400));
    }

    ScalpingDao.get_shopping_cart(par, function(err, data){
        if(err){
            return res.json(new PKG(err, 410));
        }else{
            if(data.status == 200){
                return res.json(new PKG(data));
            }else if(data.status == 201){
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/get_shopping_cart', ScalpingRouters.get_shopping_cart);

//查询每条订单商户可以获得的利润
ScalpingRouters.calculate_merchant_profit = function (req, res) {
    var par = paramAll(req);

    if (!par.bussiness_profit) {
        return res.json(new ERR('商户利润不能为空!', 400));
    }
    if (!par.gid) {
        return res.json(new ERR('商品id不能为空!', 400));
    }
    if (!par.balance_status || par.balance_status != 1) {
        return res.json(new ERR('请先给推荐人进行分润!', 400));
    }

    //查询商户登录账号
    ScalpingDao.get_merchant_account(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                par.user_id = data.userInfo.account;
                //查询用户支付宝认证信息
                ScalpingDao.get_user_aliaccount(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            par.real_name = data.userInfo.real_name;
                            par.ali_account = data.userInfo.ali_account;
                            var merchant_profit = {
                                account: par.user_id,
                                gid: par.gid,
                                ali_account: par.ali_account,
                                real_name: par.real_name,
                                bussiness_profit: par.bussiness_profit
                            };
                            //接入商户打款系统
                            return res.json(new PKG(merchant_profit));
                        } else if (data.status == 201) {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/calculate_merchant_profit', ScalpingRouters.calculate_merchant_profit);

//用户进行支付宝认证
ScalpingRouters.bind_aliAccount = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('用户账号不能为空!', 400));
    }
    if (!par.real_name) {
        return res.json(new ERR('用户真实姓名不能为空!', 400));
    }
    if (!par.ali_account) {
        return res.json(new ERR('用户支付宝账号不能为空!', 400));
    }

    ScalpingDao.bind_aliAccount(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/bind_aliAccount', ScalpingRouters.bind_aliAccount);

//查询尊享所有用户充值记录
ScalpingRouters.show_recharge_info = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.show_recharge_info(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}

//通过订单用户手机号给上级计算分润
ScalpingRouters.calculate_recommend_profit = function (req, res) {
    var par = paramAll(req);
    par.extra = 0;

    if (!par.user_id) {
        return res.json(new ERR('用户手机号不为空!', 400));
    }
    if (!par.balance) {
        return res.json(new ERR('商品佣金不为空!', 400));
    }
    if (!par.app_sid) {
        return res.json(new ERR('平台标识不为空!', 400));
    }
    if (!par.id) {
        return res.json(new ERR('订单id不为空!', 400));
    }
    if (!par.orderCode) {
        return res.json(new ERR('订单号不为空', 400));
    }

    if (par.app_sid == 'eeqqq') {
        //判断用户是否为业务员
        ScalpingDao.check_user_type(par, function (err, data) {
            if (err) {
                return res, json(new ERR(err, 410));
            } else {
                if (data.status == 200) {
                    //查找用户的五个上级，返回给前端进行展示
                } else if (data.status == 201) {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    }

    //1.通过用户手机号查找用户uid
    MallDao.phone_to_uid(par, function (err2, data2) {
        if (err2) {
            return res.json(new ERR(err2, 410));
        } else {
            if (data2.status == 200) {
                par.needAddUids = [data2.userInfo.uid];
                //2.如果存在的话查找用户的两级推荐三级代理
                MallDao.findRecAndAgentUidObj(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            //查找平台各级分润比例
                            ScalpingDao.get_classify_profit(par, function (err1, data1) {
                                if (err1) {
                                    return res.json(new ERR(err1, 410));
                                } else {
                                    if (data1.status == 200) {
                                        par.check_user_remit = [];
                                        var profitInfo = {
                                            user_id: data.msg.account,
                                            recommend_1: {
                                                account: data.msg.recommender,
                                                recommend_profit: (data1.msg.recommend_1 * par.balance).toFixed(2)
                                            },
                                            recommend_2: {
                                                account: data.msg.recommender2,
                                                recommend_profit: (data1.msg.recommend_2 * par.balance).toFixed(2)
                                            },
                                            agency_1: {
                                                account: data.msg.agent_2,
                                                recommend_profit: (data1.msg.agency_1 * par.balance).toFixed(2)
                                            },
                                            agency_2: {
                                                account: data.msg.agent_4,
                                                recommend_profit: (data1.msg.agency_2 * par.balance).toFixed(2)
                                            },
                                            agency_3: {
                                                account: data.msg.agent_6,
                                                recommend_profit: (data1.msg.agency_3 * par.balance).toFixed(2)
                                            }
                                        };

                                        if (profitInfo.recommend_1.account.length == '') {
                                            par.extra += profitInfo.recommend_1.recommend_profit
                                            delete profitInfo.recommend_1;
                                        } else {
                                            par.check_user_remit.push(profitInfo.recommend_1.account);
                                        }
                                        if (profitInfo.recommend_2.account == '') {
                                            par.extra += profitInfo.recommend_2.recommend_profit
                                            delete profitInfo.recommend_2;
                                        } else {
                                            par.check_user_remit.push(profitInfo.recommend_2.account);
                                        }
                                        if (profitInfo.agency_1.account.length == '') {
                                            par.extra += profitInfo.agency_1.recommend_profit
                                            delete profitInfo.agency_1;
                                        } else {
                                            par.check_user_remit.push(profitInfo.agency_1.account);
                                        }
                                        if (profitInfo.agency_2.account.length == '') {
                                            par.extra += profitInfo.agency_2.recommend_profit
                                            delete profitInfo.agency_2;
                                        } else {
                                            par.check_user_remit.push(profitInfo.agency_2.account);
                                        }
                                        if (profitInfo.agency_3.account.length == '') {
                                            par.extra += profitInfo.agency_3.recommend_profit
                                            delete profitInfo.agency_3;
                                        } else {
                                            par.check_user_remit.push(profitInfo.agency_3.account);
                                        }

                                        par.recommendUserCount = par.check_user_remit.length;

                                        //统计完用户利润，判断用户的上级是否都已经打款
                                        ScalpingDao.check_recommend_remit(par, function (err1, data1) {
                                            if (err1) {
                                                return res.json(new ERR(err1, 410));
                                            } else {
                                                if (data1.status == 200) {
                                                    if (par.app_sid == 'eeqqq') {
                                                        return res.json(new PKG(profitInfo));
                                                    } else {
                                                        if (par.extra > 0) {
                                                            //上级有空缺，则额外利润给商户
                                                            ScalpingDao.add_extra_profit(par, function (err, data) {
                                                                if (err) {
                                                                    return res.json(new ERR(err, 410));
                                                                } else {
                                                                    if (data.status == 200) {
                                                                        return res.json(new PKG(profitInfo));
                                                                    } else if (data.status == 201) {
                                                                        return res.json(new PKG(profitInfo));
                                                                    }
                                                                }
                                                            });
                                                        } else {
                                                            return res.json(new PKG(profitInfo));
                                                        }
                                                    }
                                                } else if (data1.status == 201) {
                                                    //更新用户状态码为1
                                                    ScalpingDao.update_balance_status(par, function (err, data) {
                                                        if (err) {
                                                            return res.json(new ERR(err, 410));
                                                        } else {
                                                            if (data.status == 200) {
                                                                return res.json(new PKG(data));
                                                            } else if (data.status == 201) {
                                                                return res.json(new ERR(data1, 420));
                                                            }
                                                        }
                                                    });
                                                }
                                            }
                                        });
                                    } else if (data.status == 201) {
                                        return res.json(new ERR(data1, 420));
                                    }
                                }
                            });
                        } else {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data2.status == 201) {
                return res.json(new ERR(data2, 420));
            }
        }
    });
}
router.post('/calculate_recommend_profit', ScalpingRouters.calculate_recommend_profit);

//业务员代金券分润
ScalpingRouters.get_operation_profit = function (req, res) {
    var par = paramAll(req);
    var user = [];
    var voucher = [];

    if (!par.user_id) {
        return res.json(new ERR({
            msg: '业务员手机号不为空!'
        }, 400));
    }
    if (!par.voucher) {
        return res.json(new ERR({
            msg: '业务员代金券佣金不为空!'
        }, 400));
    }

    //1.通过用户手机号查找用户uid
    MallDao.phone_to_uid(par, function (err2, data2) {
        if (err2) {
            return res.json(new ERR(err2, 410));
        } else {
            if (data2.status == 200) {
                par.needAddUids = [data2.userInfo.uid];
                //2.如果存在的话查找用户的两级推荐三级代理
                MallDao.findRecAndAgentUidObj(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            delete data.msg.account;
                            for (var user_phone in data.msg) {
                                if (data.msg[user_phone].length > 6) {
                                    user.push(data.msg[user_phone]);
                                }
                            }
                            console.log(user);
                            user.forEach(user_phone => {
                                ScalpingDao.get_user_scale(user_phone, function (err1, data1) {
                                    if (err1) {
                                        return res.json(new ERR(err1, 410));
                                    } else {
                                        if (data1.status == 200) {
                                            //查找用户可得利润
                                            voucher.push({user: user_phone, rank: rank_to_voucher(data1.msg) * par.voucher, tk_Rate: data1.msg});
                                            if(voucher.length == user.length){
                                                return res.json(new PKG({status: 200, msg: voucher}));
                                            }
                                        } else if (data1.status == 201) {
                                            return res.json(new ERR(data1, 420));
                                        }
                                    }
                                });
                            });
                        } else {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data2.status == 201) {
                return res.json(new ERR(data2, 420));
            }
        }
    });
}
router.post('/get_operation_profit', ScalpingRouters.get_operation_profit);

//判断上级是否已全部打款
ScalpingRouters.check_recommend_remit = function (req, res) {
    var par = paramAll(req);
    par.check_user_remit = [];

    if (!par.user_id) {
        return res.json(new ERR({
            msg: '用户手机号不为空!'
        }, 400));
    }
    if (!par.orderCode) {
        return res.json(new ERR({
            msg: '订单号不能为空!'
        }, 400));
    }
    if (!par.user_type) {
        return res.json(new ERR({
            msg: '订单号不能为空!'
        }, 400));
    }

    //1.通过用户手机号查找用户uid
    MallDao.phone_to_uid(par, function (err2, data2) {
        if (err2) {
            return res.json(new ERR(err2, 410));
        } else {
            if (data2.status == 200) {
                par.needAddUids = [data2.userInfo.uid];
                //2.如果存在的话查找用户的两级推荐三级代理
                MallDao.findRecAndAgentUidObj(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            console.log('-----------------------');
                            console.log(data);
                            if (data.msg.recommender != "") {
                                par.check_user_remit.push(data.msg.recommender);
                            }
                            if (data.msg.recommender2 != "") {
                                par.check_user_remit.push(data.msg.recommender2);
                            }
                            if (data.msg.agent_2 != "") {
                                par.check_user_remit.push(data.msg.agent_2);
                            }
                            if (data.msg.agent_4 != "") {
                                par.check_user_remit.push(data.msg.agent_4);
                            }
                            if (data.msg.agent_6 != "") {
                                par.check_user_remit.push(data.msg.agent_6);
                            }

                            par.recommendUserCount = par.check_user_remit.length;
                            console.log(par.check_user_remit);
                            console.log(par.recommendUserCount);
                            //判断用户的上级是否都已经打款
                            ScalpingDao.check_recommend_remit(par, function (err1, data1) {
                                if (err1) {
                                    return res.json(new ERR(err1, 410));
                                } else {
                                    if (data1.status == 200) {
                                        return res.json(new PKG('用户还未全部打款!'));
                                    } else if (data1.status == 201) {
                                        //更新用户状态码为1
                                        ScalpingDao.update_balance_status(par, function (err, data) {
                                            if (err) {
                                                return res.json(new ERR(err, 410));
                                            } else {
                                                if (data.status == 200) {
                                                    return res.json(new ERR(data, 420));
                                                } else if (data.status == 201) {
                                                    return res.json(new ERR(data1, 420));
                                                }
                                            }
                                        });
                                    }
                                }
                            });
                        } else {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data2.status == 201) {
                return res.json(new ERR(data2, 420));
            }
        }
    });
}
router.post('/check_recommend_remit', ScalpingRouters.check_recommend_remit);

//接入支付宝打款
ScalpingRouters.checkAliaccountExistAndRemit = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('用户账号不为空!', 400));
    }
    if (!par.balance) {
        return res.json(new ERR('用户利润不为空!', 400));
    }
    if (!par.orderCode) {
        return res.json(new ERR('订单号不为空!', 400));
    }
    if (!par.user_type) {
        return res.json(new ERR('用户类型不能为空!', 400));
    }
    if (par.user_type == 0) {
        par.payer_show_name = '用户分享商品奖励佣金';
    } else if (par.user_type == 1) {
        par.payer_show_name = '橱窗商户卖出商品盈利';
    } else if (par.user_type == 2) {
        par.payer_show_name = '业务员购买商品代理盈利';
    } else if (par.user_type == 3) {
        par.payer_show_name = '业务员代金券代理盈利';
    } else {
        return res.json(new ERR('用户类型有误!', 400));
    }

    ScalpingDao.get_user_aliaccount(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                par.ali_account = data.userInfo.ali_account;
                par.real_name = data.userInfo.real_name;

                ScalpingDao.check_remit_exist(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (data.status == 200) {
                            var remitInfo = {
                                orderCode: par.orderCode,
                                user_id: par.ali_account,
                                balance: par.balance,
                                payer_show_name: par.payer_show_name,
                                payee_real_name: par.real_name,
                                user_type: par.user_type
                            }

                            ScalpingDao.forword_transfer_commission(remitInfo, function (err1, data1) {
                                if (err1) {
                                    return res.json(new ERR(err1, 400));
                                } else {
                                    if (data1.status == 200) {
                                        return res.json(new PKG(data1));
                                    } else if (data1.status == 420) {
                                        return res.json(new ERR(data1, 420));
                                    }
                                }
                            });
                        } else if (data.status == 201) {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/checkAliaccountExistAndRemit', ScalpingRouters.checkAliaccountExistAndRemit);

//获取我的小伙伴
ScalpingRouters.get_friend_list = function (req, res) {
    var par = paramAll(req);

    if (!par.user_id) {
        return res.json(new ERR('用户账号不为空!', 400));
    }

    ScalpingDao.get_friend_list(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/get_friend_list', ScalpingRouters.get_friend_list);

//同步金豆商城数据(循环更新)
ScalpingRouters.sync_goods_test = function (req, res) {
    var par = paramAll(req);

    if (!par.synckey) {
        par.synckey = 0;
    }

    ScalpingDao.syncGoods(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (par.status == 200) { //接口上架新商品
                par.updatekey = data.synckey;
                //判断商品id是否已经存在
                if (par.updatekey != 0) {
                    data.data.forEach(data => {
                        if (data.publish_date == '0000-00-00 00:00:00') {
                            data.publish_date = new Date();
                        }
                        data.created_date = format(data.created_date);
                        data.publish_date = format(data.publish_date);
                        ScalpingDao.check_goods_exist(data, function (err1, data1) {
                            if (err1) {
                                console.log(err1);
                            } else {
                                if (data1.status == 200) { //商品不存在，将上架商品更新到商品库
                                    ScalpingDao.saveGoods(data, function (err2, data2) {
                                        if (err2) {
                                            return res.json(new ERR(err2, 410));
                                        } else {
                                            if (data2.status == 200) {
                                                // console.log(data2);
                                            } else if (data2.status == 201) {
                                                console.log(data2);
                                            }
                                        }
                                    });
                                } else if (data1.status == 201) { //商品存在，判断商品库存量是否改变，改变则更新
                                    if (data1.totalNum == data.totalNum) {
                                        console.log('商品已存在，库存量未更新!' + data.id);
                                    } else {
                                        ScalpingDao.changed_totalNum(data, function (err2, data2) {
                                            if (err2) {
                                                console.log(err2);
                                            } else {
                                                if (data2.status == 200) {
                                                    console.log(data2);
                                                } else if (data2.status == 201) {
                                                    console.log(data2);
                                                }
                                            }
                                        });
                                    }
                                }
                            }
                        });
                    });
                }

                //每次更新一百条数据，更新完将接口返回的synckey作为新的参数又一次调用原接口
                ScalpingDao.update_key(par, function (err3, data3) {
                    if (err3) {
                        console.log(err3);
                    } else {

                    }
                });
            } else if (par.status == 201) { //当前无最新上架商品
                return res.json(new ERR(data));
            } else if (par.status == 202) { //当前无最新上架商品
                return res.json(new ERR(data));
            }
        }
    });
}
router.post('/sync_goods_test', ScalpingRouters.sync_goods_test);

//金豆商城商品列表
ScalpingRouters.get_zd_goods = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.get_zd_goods(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 400) {
                return res.json(new ERR(data, 400));
            }
        }
    });
}
router.post('/get_zd_goods', ScalpingRouters.get_zd_goods);

//添加收货地址
ScalpingRouters.save_addr = function (req, res) {
    var par = paramAll(req);

    if (!par.province) {
        return res.json(new ERR('省份不能为空!', 400));
    }
    if (!par.city) {
        return res.json(new ERR('城市不能为空!', 400));
    }
    if (!par.street) {
        return res.json(new ERR('街道不能为空!', 400));
    }
    if (!par.detail) {
        return res.json(new ERR('详细地址不能为空!', 400));
    }
    if (!par.post_code) {
        return res.json(new ERR('邮编不能为空!', 400));
    }
    if (!par.name) {
        return res.json(new ERR('收件人姓名不能为空!', 400));
    }
    if (!par.tel) {
        return res.json(new ERR('收件人电话不能为空!', 400));
    }
    if (!par.login_account) {
        par.login_account = par.tel;
    }
    if (!par.is_default || par.is_default != 1) {
        par.is_default = 0;
    }
    if (par.is_default == 1) {
        ScalpingDao.update_default_addr(par, function (err2, data2) {
            if (err2) {
                return res.json(new ERR(err2, 410));
            } else {
                if (par.status == 200) {
                    ScalpingDao.save_addr(par, function (err, data) {
                        if (err) {
                            return res.json(new ERR(err, 410));
                        } else {
                            if (data.status == 200) {
                                par.addr_id = data.payload.id;
                                ScalpingDao.save_addr_plat(par, function (err1, data1) {
                                    if (err1) {
                                        return res.json(new ERR(err1, 410));
                                    } else {
                                        if (par.status == 200) {
                                            return res.json(new PKG(data));
                                        } else if (par.status == 201) {
                                            return res.json(new ERR(data1, 420));
                                        }
                                    }
                                });
                            } else if (data.status == 400) {
                                return res.json(new ERR(data, 410));
                            }
                        }
                    });
                } else {
                    return res.json(new ERR(data2, 420));
                }
            }
        });
    } else {
        ScalpingDao.save_addr(par, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (data.status == 200) {
                    console.log(data);
                    par.addr_id = data.payload.id;
                    ScalpingDao.save_addr_plat(par, function (err1, data1) {
                        if (err1) {
                            return res.json(new ERR(err1, 410));
                        } else {
                            if (par.status == 200) {
                                return res.json(new PKG(data));
                            } else if (par.status == 201) {
                                return res.json(new ERR(data1, 420));
                            }
                        }
                    });
                } else if (data.status == 400) {
                    return res.json(new ERR(data, 410));
                }
            }
        });
    }
}
router.post('/save_addr', ScalpingRouters.save_addr);

//查看地址列表
ScalpingRouters.addr_list = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.addr_list(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 400) {
                return res.json(new ERR(data, 400));
            }
        }
    });
}
router.post('/addr_list', ScalpingRouters.addr_list);

//查看用户的地址列表
ScalpingRouters.addr_list_plat = function (req, res) {
    var par = paramAll(req);

    if (!par.account) {
        return res.json(new ERR('手机号不能为空!', 420));
    }

    ScalpingDao.get_addr_default(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (par.status == 200) {
                for (i = 0; i < data.length; i++) {
                    data[i].addr = data[i].province + data[i].city + data[i].street + data[i].detail;
                }

                return res.json(new PKG(data));
            } else if (par.status == 201) {
                return res.json(new ERR(data));
            }
        }
    });
}
router.post('/addr_list_plat', ScalpingRouters.addr_list_plat);

//返回一个地址(有默认地址则返回默认地址)
ScalpingRouters.get_addr = function (req, res) {
    var par = paramAll(req);

    if (!par.account) {
        return res.json(new ERR('未登录，无默认地址!', 420));
    }

    //调用自己接口取得用户一个地址，有默认地址则返回默认地址
    ScalpingDao.get_addr_default(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (par.status == 200) {
                for (i = 0; i < data.length; i++) {
                    if (data[i].is_default == '1') {
                        data[i].addr = data[i].province + data[i].city + data[i].street + data[i].detail;
                        return res.json(new PKG(data[i]));
                    }
                }
                data[0].addr = data[0].province + data[0].city + data[0].street + data[0].detail;
                return res.json(new PKG(data[0]));
            } else if (par.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/get_addr', ScalpingRouters.get_addr);

//删除地址
ScalpingRouters.del_addr = function (req, res) {
    var par = paramAll(req);

    if (!par.id) {
        return res.json(new ERR('地址id不能为空!', 420));
    }

    ScalpingDao.del_addr(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                ScalpingDao.del_addr_plat(par, function (err, data) {
                    if (err) {
                        return res.json(new ERR(err, 410));
                    } else {
                        if (par.status == 200) {
                            return res.json(new PKG(data));
                        } else if (par.status == 201) {
                            return res.json(new ERR(data, 420));
                        }
                    }
                });
            } else if (data.status == 400) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/del_addr', ScalpingRouters.del_addr);

//创建订单
ScalpingRouters.create_plat_order = function (req, res) {
    var par = paramAll(req);

    ScalpingDao.create_plat_order(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                return res.json(new PKG(data));
            } else if (data.status == 400) {
                return res.json(new ERR(data, 400));
            }
        }
    });
}
router.post('/create_plat_order', ScalpingRouters.create_plat_order);

//商品id查询详细信息
ScalpingRouters.id_to_goodsinfo = function (req, res) {
    var par = paramAll(req);

    console.log(par);

    if (!par.id) {
        return res.json(new ERR('商品id不能为空', 400));
    } else {
        ScalpingDao.id_to_goodsinfo(par, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (par.status == 200) {
                    if (data.showcase_status == 2) {
                        if (data.pictUrl) {
                            data.pictUrl = data.pictUrl.indexOf('http') >= 0 ? data.pictUrl : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pictUrl;
                        }
                        if (data.pics) {
                            data.pics = JSON.parse(data.pics);

                            for (i = 0; i < data.pics.length; i++) {
                                data.pics[i] = data.pics[i].indexOf('http') >= 0 ? data.pics[i] : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pics[i];
                            }
                        }
                        if (data.detail) {
                            data.detail = JSON.parse(data.detail);

                            for (i = 0; i < data.detail.pics.length; i++) {
                                data.detail.pics[i] = data.detail.pics[i].indexOf('http') >= 0 ? data.detail.pics[i] : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.detail.pics[i];
                            }
                        }
                    } else {
                        if (data.pictUrl) {
                            data.pictUrl = data.pictUrl.indexOf('http') >= 0 ? data.pictUrl : 'http://pic.kuailezhizao.com' + data.pictUrl;
                        }
                        if (data.pics) {
                            data.pics = JSON.parse(data.pics);

                            for (i = 0; i < data.pics.length; i++) {
                                data.pics[i] = data.pics[i].indexOf('http') >= 0 ? data.pics[i] : 'http://pic.kuailezhizao.com' + data.pics[i];
                            }
                        }
                        if (data.detail) {
                            data.detail = JSON.parse(data.detail);

                            for (i = 0; i < data.detail.pics.length; i++) {
                                data.detail.pics[i] = data.detail.pics[i].indexOf('http') >= 0 ? data.detail.pics[i] : 'http://pic.kuailezhizao.com' + data.detail.pics[i];
                            }
                        }
                    }

                    if (data.sku) {
                        try {
                            data.sku = JSON.parse(data.sku);
                        } catch (e) {
                            return res.json(new ERR('商品不存在!', 410));
                        }
                    }

                    return res.json(new PKG(data));
                } else if (par.status == 201) {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    }
}
router.post('/id_to_goodsinfo', ScalpingRouters.id_to_goodsinfo);

//根据商品具体规格查询尚有销量的商品id
ScalpingRouters.specificate_to_id = function (req, res) {
    var par = paramAll(req);

    if (!par.sku_value) {
        return res.json(new ERR('商品具体规格不为空!', 400));
    }
    if (!par.id) {
        return res.json(new ERR('商品主类id不为空!', 400));
    }

    ScalpingDao.specificate_to_id(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            if (data.status == 200) {
                if (data.showcase_status == 2) {
                    data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pics;
                } else {
                    data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'http://pic.kuailezhizao.com' + data.pics;
                }

                return res.json(new PKG(data));
            } else if (data.status == 201) {
                return res.json(new ERR(data, 420));
            }
        }
    });
}
router.post('/specificate_to_id', ScalpingRouters.specificate_to_id);

//选择商品规格返回该规格具体销量
ScalpingRouters.sku_total_count = function (req, res) {
    var par = paramAll(req);

    if (!par.id) {
        return res.json(new ERR('请联系管理员!', 400));
    }

    ScalpingDao.check_sku_length(par, function (err, data) {
        if (err) {
            return res.json(new ERR(err, 410));
        } else {
            data.sku = JSON.parse(data.sku);
            if (par.status == 200) {
                if (getJsonObjLength(data.sku) == 2) {
                    if (!par.color || !par.size) {
                        return res.json(new ERR('请完整选择规格', 400));
                    } else {
                        ScalpingDao.sku_total_count(par, function (err, data) {
                            if (err) {
                                return res.json(new ERR(err, 410));
                            } else {
                                if (par.status == 200) {
                                    if (data.pics) {
                                        if (data.showcase_status == 2) {
                                            data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pics;
                                        } else {
                                            data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'http://pic.kuailezhizao.com' + data.pics;
                                        }
                                    }
                                    return res.json(new PKG(data));
                                } else if (par.status == 201) {
                                    ScalpingDao.sku_total_count_opposite(par, function (err1, data1) {
                                        if (err1) {
                                            return res.json(new ERR(err1, 410));
                                        } else {
                                            if (par.status == 200) {
                                                if (data1.pics) {
                                                    if (data1.showcase_status == 2) {
                                                        data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data1.pics;
                                                    } else {
                                                        data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'http://pic.kuailezhizao.com' + data1.pics;
                                                    }
                                                }
                                                return res.json(new PKG(data1));
                                            } else if (par.status == 201) {
                                                return res.json(new ERR(data1, 420));
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                } else if (getJsonObjLength(data.sku) == 1) {
                    if (!par.color && !par.size) {
                        return res.json(new ERR('请先选择规格', 400));
                    } else {
                        ScalpingDao.sku_total_count(par, function (err, data) {
                            if (err) {
                                return res.json(new ERR(err, 410));
                            } else {
                                if (par.status == 200) {
                                    if (data.pics) {
                                        if (data.showcase_status == 2) {
                                            data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pics;
                                        } else {
                                            data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'http://pic.kuailezhizao.com' + data.pics;
                                        }
                                    }
                                    return res.json(new PKG(data));
                                } else if (par.status == 201) {
                                    ScalpingDao.sku_total_count_opposite(par, function (err1, data1) {
                                        if (err1) {
                                            return res.json(new ERR(err1, 410));
                                        } else {
                                            if (par.status == 200) {
                                                if (data1.pics) {
                                                    if (data1.showcase_status == 2) {
                                                        data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data1.pics;
                                                    } else {
                                                        data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'http://pic.kuailezhizao.com' + data1.pics;
                                                    }
                                                }
                                                return res.json(new PKG(data1));
                                            } else if (par.status == 201) {
                                                return res.json(new ERR(data1, 420));
                                            }
                                        }
                                    });
                                }
                            }
                        });
                    }
                } else {
                    ScalpingDao.sku_total_count(par, function (err, data) {
                        if (err) {
                            return res.json(new ERR(err, 410));
                        } else {
                            if (par.status == 200) {
                                if (data.pics) {
                                    if (data.showcase_status == 2) {
                                        data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data.pics;
                                    } else {
                                        data.pics = data.pics.indexOf('http') >= 0 ? data.pics : 'http://pic.kuailezhizao.com' + data.pics;
                                    }
                                }
                                return res.json(new PKG(data));
                            } else if (par.status == 201) {
                                ScalpingDao.sku_total_count_opposite(par, function (err1, data1) {
                                    if (err1) {
                                        return res.json(new ERR(err1, 410));
                                    } else {
                                        if (par.status == 200) {
                                            if (data1.pics) {
                                                if (data1.showcase_status == 2) {
                                                    data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data1.pics;
                                                } else {
                                                    data1.pics = data1.pics.indexOf('http') >= 0 ? data1.pics : 'http://pic.kuailezhizao.com' + data1.pics;
                                                }
                                            }
                                            return res.json(new PKG(data1));
                                        } else if (par.status == 201) {
                                            return res.json(new ERR(data1, 420));
                                        }
                                    }
                                });
                            }
                        }
                    });
                }
            } else if (par.status == 201) {
                return res.json(new ERR(data));
            }
        }
    });
}
router.post('/sku_total_count', ScalpingRouters.sku_total_count);

//商品分类测试
ScalpingRouters.goods_classify = function (req, res) {
    var par = paramAll(req);
    console.log(par);

    if (par.rank != 'sale') {
        return res.json(new ERR('rank参数有误!', 400));
    }
    if (!par.rank) {
        return res.json(new ERR('rank不能为空!', 400));
    } else {
        ScalpingDao.goods_classify(par, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (par.status == 200) {
                    for (j = 0; j < data.length; j++) {
                        if (data[j].pictUrl) {
                            data[j].pictUrl = data[j].pictUrl.indexOf('http') >= 0 ? data[j].pictUrl : 'http://pic.kuailezhizao.com' + data[j].pictUrl;
                        }
                        if (data[j].is_publish) {
                            data[j].is_publish = data[j].is_publish.indexOf('http') >= 0 ? data[j].is_publish : 'http://pic.kuailezhizao.com' + data[j].is_publish;
                        }
                        if (data[j].pics) {
                            data[j].pics = JSON.parse(data[j].pics);
                            for (i = 0; i < data[j].pics.length; i++) {
                                data[j].pics[i] = data[j].pics[i].indexOf('http') >= 0 ? data[j].pics[i] : 'http://pic.kuailezhizao.com' + data[j].pics[i];
                            }
                        }
                        if (data[j].detail) {
                            data[j].detail = JSON.parse(data[j].detail);
                            for (i = 0; i < data[j].detail.pics.length; i++) {
                                data[j].detail.pics[i] = data[j].detail.pics[i].indexOf('http') >= 0 ? data[j].detail.pics[i] : 'http://pic.kuailezhizao.com' + data[j].detail.pics[i];
                            }
                        }
                    }

                    ScalpingDao.get_goods_count_classify(par, function (err1, data1) {
                        if (err1) {
                            return res.json(new ERR(err1, 410));
                        } else {
                            if (par.status == 200) {
                                return res.json(new PKG({
                                    data: data,
                                    goods_count: data1.goods_count
                                }));
                            } else if (par.status == 201) {
                                return res.json(new ERR(data1, 420));
                            }
                        }
                    });
                } else if (par.status == 201) {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    }
}
router.post('/goods_classify', ScalpingRouters.goods_classify);

//商品分类
ScalpingRouters.goods_classify_gid = function (req, res) {
    var par = paramAll(req);

    if (!par.aid) {
        return res.json(new ERR('gid不能为空!', 400));
    } else {
        par.aid = change_keyword(par.aid);

        if (par.app_sid) {
            par.app_sid = change_appSid(par.app_sid);
        }

        ScalpingDao.goods_classify_gid(par, function (err, data) {
            if (err) {
                return res.json(new ERR(err, 410));
            } else {
                if (par.status == 200) {
                    for (j = 0; j < data.length; j++) {
                        if (data[j].showcase_status != 2) {
                            if (data[j].pictUrl) {
                                data[j].pictUrl = data[j].pictUrl.indexOf('http') >= 0 ? data[j].pictUrl : 'http://pic.kuailezhizao.com' + data[j].pictUrl;
                            }
                            if (data[j].pics) {
                                data[j].pics = JSON.parse(data[j].pics);
                                for (i = 0; i < data[j].pics.length; i++) {
                                    data[j].pics[i] = data[j].pics[i].indexOf('http') >= 0 ? data[j].pics[i] : 'http://pic.kuailezhizao.com' + data[j].pics[i];
                                }
                            }
                            if (data[j].detail) {
                                data[j].detail = JSON.parse(data[j].detail);
                                for (i = 0; i < data[j].detail.pics.length; i++) {
                                    data[j].detail.pics[i] = data[j].detail.pics[i].indexOf('http') >= 0 ? data[j].detail.pics[i] : 'http://pic.kuailezhizao.com' + data[j].detail.pics[i];
                                }
                            }
                        } else {
                            if (data[j].pictUrl) {
                                data[j].pictUrl = data[j].pictUrl.indexOf('http') >= 0 ? data[j].pictUrl : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data[j].pictUrl;
                            }
                            if (data[j].pics) {
                                data[j].pics = JSON.parse(data[j].pics);
                                for (i = 0; i < data[j].pics.length; i++) {
                                    data[j].pics[i] = data[j].pics[i].indexOf('http') >= 0 ? data[j].pics[i] : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data[j].pics[i];
                                }
                            }
                            if (data[j].detail) {
                                data[j].detail = JSON.parse(data[j].detail);
                                for (i = 0; i < data[j].detail.pics.length; i++) {
                                    data[j].detail.pics[i] = data[j].detail.pics[i].indexOf('http') >= 0 ? data[j].detail.pics[i] : 'https://usuz.oss-cn-shenzhen.aliyuncs.com' + data[j].detail.pics[i];
                                }
                            }
                        }
                    }

                    ScalpingDao.get_goods_count_classify_gid(par, function (err1, data1) {
                        if (err1) {
                            return res.json(new ERR(err1, 410));
                        } else {
                            if (par.status == 200) {
                                return res.json(new PKG({
                                    data: data,
                                    goods_count: data1.goods_count
                                }));
                            } else if (par.status == 201) {
                                return res.json(new ERR(data1, 420));
                            }
                        }
                    });
                } else if (par.status == 201) {
                    return res.json(new ERR(data, 420));
                }
            }
        });
    }
}
router.post('/goods_classify_gid', ScalpingRouters.goods_classify_gid);

module.exports = router;